import _regeneratorRuntime from "@babel/runtime/regenerator";
import _Stores from "choerodon-ui/pro/lib/stores";
import _DataSet from "choerodon-ui/pro/lib/data-set";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _isObject from "lodash/isObject";
import _omit from "lodash/omit";
import _map from "lodash/map";
import { OPERATOR_TYPE } from "../../constant";

function findLovParam(_x, _x2, _x3) {
  return _findLovParam.apply(this, arguments);
}
/**
 * 字段改变事件
 * @param record
 * @param name
 * @param value
 * @param fieldArr
 */


function _findLovParam() {
  _findLovParam = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(fieldArr, name, value) {
    var _fieldArr$find, lovCode, valueField, config;

    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _fieldArr$find = fieldArr.find(function (field) {
              return field.value === name;
            }), lovCode = _fieldArr$find.lovCode, valueField = _fieldArr$find.valueField;

            if (!lovCode) {
              _context.next = 11;
              break;
            }

            if (!lovCode) {
              _context.next = 11;
              break;
            }

            if (valueField) {
              _context.next = 10;
              break;
            }

            _context.next = 6;
            return _Stores.LovCodeStore.fetchConfig(lovCode);

          case 6:
            config = _context.sent;
            return _context.abrupt("return", value[config.valueField]);

          case 10:
            return _context.abrupt("return", value[valueField]);

          case 11:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _findLovParam.apply(this, arguments);
}

function handleFieldChange(_x4, _x5) {
  return _handleFieldChange.apply(this, arguments);
}

function _handleFieldChange() {
  _handleFieldChange = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(_ref, fieldArr) {
    var record, name, value, _ref3, defaultValue, lovCode, lovValue;

    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            record = _ref.record, name = _ref.name, value = _ref.value;
            _ref3 = fieldArr.find(function (field) {
              return field.value === record.get('fieldName');
            }) || {}, defaultValue = _ref3.defaultValue, lovCode = _ref3.lovCode;

            if (name === 'fieldName') {
              record.set('comparator', '');
              record.set('value', undefined);

              _map(fieldArr, function (field) {
                if (value === field.value) {
                  record.getField('comparator').set('options', new _DataSet({
                    selection: 'single',
                    data: OPERATOR_TYPE[field.type.toUpperCase()]
                  }));
                }
              });
            }

            if (name === 'comparator') {
              if (value && value.indexOf('NULL') !== -1) {
                record.set('value', undefined);
              } else if (defaultValue) {
                record.set('value', defaultValue);
              }
            }

            if (!(name === 'value' && _isObject(value) && lovCode)) {
              _context2.next = 9;
              break;
            }

            _context2.next = 7;
            return findLovParam(fieldArr, record.get('fieldName'), value);

          case 7:
            lovValue = _context2.sent;
            record.set('value', lovValue);

          case 9:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _handleFieldChange.apply(this, arguments);
}

function getDynamicProps(dataSet, record) {
  if (record.get('fieldName')) {
    var tableField = dataSet.getField(record.get('fieldName'));
    return tableField ? _omit(tableField.props, ['name', 'label', 'required', 'bind', 'ignore', 'validator', 'readOnly', 'cascadeMap']) : {};
  }

  return {};
}

export default (function (dataSet, fieldArr) {
  return {
    paging: false,
    fields: [{
      name: 'fieldName',
      type: 'string',
      label: '字段',
      unique: true,
      options: new _DataSet({
        selection: 'single',
        paging: false,
        data: fieldArr
      }),
      required: true
    }, {
      name: 'comparator',
      type: 'string',
      label: '比较符',
      options: new _DataSet({
        selection: 'single',
        paging: false,
        data: OPERATOR_TYPE.ALL
      }),
      required: true
    }, {
      name: 'value',
      label: '比较值',
      dynamicProps: function dynamicProps(_ref2) {
        var record = _ref2.record;
        return getDynamicProps(dataSet, record);
      }
    }, {
      name: 'searchConditionId',
      type: 'number'
    }],
    events: {
      update: function update(props) {
        return handleFieldChange(props, fieldArr);
      }
    }
  };
});